/**
 * @author 黄波
 * 创建日期：2011Sep 4, 2011
 * TODO// N/A
 */
package com.platform.os.util;

import java.io.File;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

public class Util {

	/**
	 * 解析字符串
	 * @param date
	 * @return
	 */
	public static String parseDate(Date date, String pattern) {
		if(date == null) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}
	
	/**
	 * 获得请求
	 * @return
	 */
	public static HttpServletRequest getReq() {
		return ServletActionContext.getRequest();
	}
	
	
	/**
	 * 获得应答
	 * @return
	 */
	public static HttpServletResponse getResp() {
		return ServletActionContext.getResponse();
	}
	
	/**
	 * 获得输出流
	 * @return
	 */
	public static PrintWriter getOut() throws Exception {
		return ServletActionContext.getResponse().getWriter();
	}
	
	/**
	 * 根据map获得json格式数据
	 * @param map
	 * @return
	 */
	public static JSONObject getJson(Map map) {
		return JSONObject.fromObject(map);
	}
	
	public static void clearTMPFolder() {
		File file = new File(ServletActionContext.getServletContext().getAttribute("javax.servlet.context.tempdir").toString());
		if(file.exists()) {
			file.delete();
		}
	
	}
}
